package com.sinbader.xiaole.common.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import com.sinbader.xiaole.common.entity.InStockInfo;
import com.sinbader.xiaole.common.mapper.InStockInfoMapper;
import com.sinbader.xiaole.common.model.params.InStockInfoParam;
import com.sinbader.xiaole.common.model.result.InStockInfoResult;
import  com.sinbader.xiaole.common.service.InStockInfoService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * 入库单（采购入库、调拨入库） 服务实现类
 */
@Service
public class InStockInfoServiceImpl extends ServiceImpl<InStockInfoMapper, InStockInfo> implements InStockInfoService {

    @Override
    public void add(InStockInfoParam param){
        InStockInfo entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(InStockInfoParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(InStockInfoParam param){
        InStockInfo oldEntity = getOldEntity(param);
        InStockInfo newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public InStockInfoResult findBySpec(InStockInfoParam param){
        return null;
    }

    @Override
    public List<InStockInfoResult> findListBySpec(InStockInfoParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(InStockInfoParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(InStockInfoParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private InStockInfo getOldEntity(InStockInfoParam param) {
        return this.getById(getKey(param));
    }

    private InStockInfo getEntity(InStockInfoParam param) {
        InStockInfo entity = new InStockInfo();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
